package com.dd.ljzapplication.activity;

import com.dd.ljzapplication.R;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;

import base.BaseUtilActivity;
import okhttp3.OkHttpClient;

/**
 * Created by LiJZ on 2016/12/6.
 */

public class HttpsActivity extends BaseUtilActivity {

	private String CER_12306 = "-----BEGIN CERTIFICATE-----\n" +
			"MIICmjCCAgOgAwIBAgIIbyZr5/jKH6QwDQYJKoZIhvcNAQEFBQAwRzELMAkGA1UEBhMCQ04xKTAn\n" +
			"BgNVBAoTIFNpbm9yYWlsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRTUkNBMB4X\n" +
			"DTA5MDUyNTA2NTYwMFoXDTI5MDUyMDA2NTYwMFowRzELMAkGA1UEBhMCQ04xKTAnBgNVBAoTIFNp\n" +
			"bm9yYWlsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MQ0wCwYDVQQDEwRTUkNBMIGfMA0GCSqGSIb3\n" +
			"DQEBAQUAA4GNADCBiQKBgQDMpbNeb34p0GvLkZ6t72/OOba4mX2K/eZRWFfnuk8e5jKDH+9BgCb2\n" +
			"9bSotqPqTbxXWPxIOz8EjyUO3bfR5pQ8ovNTOlks2rS5BdMhoi4sUjCKi5ELiqtyww/XgY5iFqv6\n" +
			"D4Pw9QvOUcdRVSbPWo1DwMmH75It6pk/rARIFHEjWwIDAQABo4GOMIGLMB8GA1UdIwQYMBaAFHle\n" +
			"tne34lKDQ+3HUYhMY4UsAENYMAwGA1UdEwQFMAMBAf8wLgYDVR0fBCcwJTAjoCGgH4YdaHR0cDov\n" +
			"LzE5Mi4xNjguOS4xNDkvY3JsMS5jcmwwCwYDVR0PBAQDAgH+MB0GA1UdDgQWBBR5XrZ3t+JSg0Pt\n" +
			"x1GITGOFLABDWDANBgkqhkiG9w0BAQUFAAOBgQDGrAm2U/of1LbOnG2bnnQtgcVaBXiVJF8LKPaV\n" +
			"23XQ96HU8xfgSZMJS6U00WHAI7zp0q208RSUft9wDq9ee///VOhzR6Tebg9QfyPSohkBrhXQenvQ\n" +
			"og555S+C3eJAAVeNCTeMS3N/M5hzBRJAoffn3qoYdAO1Q8bTguOi+2849A==\n" +
			"-----END CERTIFICATE-----";

	@Override
	protected int setLayoutID() {
		return R.layout.activity_https;
	}

	@Override
	protected void init() {

	}

	@Override
	protected void initActionBar() {

	}

	@Override
	protected void initView() {

	}

	@Override
	protected void initEvent() {

	}

	public void setCertificates(InputStream... certificates) {
//		try
//		{
//		CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
//		KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
//		keyStore.load(null);
//		        int index = 0;
//		        for (InputStream certificate : certificates)
//		        {
//			            String certificateAlias = Integer.toString(index++);
//			            keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));
//			 
//			            try
//			            {
//				                if (certificate != null)
//				                    certificate.close();
//				            } catch (IOException e)
//			            {
//				            }
//			        }
//		 
//		        SSLContext sslContext = SSLContext.getInstance("TLS");
//		 
//		        TrustManagerFactory trustManagerFactory = 
//		            TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 
//		 
//		        trustManagerFactory.init(keyStore);
//		        sslContext.init
//		            (   
//		                null, 
//		                trustManagerFactory.getTrustManagers(), 
//		                new SecureRandom()
//		            );
//		       mOkHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
//		 
//		 
//		    } catch (Exception e)
//		    {
//		        e.printStackTrace();
//		    } 
//		 

		try {
			CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
			KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
			keyStore.load(null);
			int index = 0;
			for (InputStream certificate : certificates) {
				String certificateAlias = Integer.toString(index++);
				keyStore.setCertificateEntry(certificateAlias, certificateFactory.generateCertificate(certificate));

				try {
					if (certificate != null) {
						certificate.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			SSLContext sslContext = SSLContext.getInstance("TLS");
			TrustManagerFactory trustManagerFactory =
					TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
			trustManagerFactory.init(keyStore);
			sslContext.init(null,trustManagerFactory.getTrustManagers(),new SecureRandom());
			OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
					.sslSocketFactory(sslContext.getSocketFactory())
					.build();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
